/**
 * app路由集合页
 */

import { Router } from 'express';
import receiveAuthRoutes from './receivePush';
import receiveMsgRoutes from './receiveMsg';
import apiRoutes from './api';
import authSSOFilter from '../middleware/auth-sso-filter';
import authAesFilter from '../middleware/auth-aes-filter';
import adminRoutes from './admin';
import extractRoutes from './extract';
const router = Router();

// api请求允许跨域 | Authorization 验证
router.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', req.headers.origin); // *
  res.header('Access-Control-Allow-Credentials', 'true'); // cookie
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Authorization, Content-Type, Accept ,systemId, Platform'
  );
  res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  if (req.method === 'OPTIONS') return res.status(200).end(); // 加快响应速度
  next();
});

// 路由
router.post('/:appID/callback', receiveMsgRoutes); // 消息与事件接受(use无法传递params)
router.use('/callback', receiveAuthRoutes); // 授权事件接受

// 管理端
// router.use('/login', loginRoutes);// 测试使用
router.use('/admin', adminRoutes); // 刷数据

// api相关路由
router.use('/api', authSSOFilter, apiRoutes);

//外部服务器端接口调用
router.use('/extract', authAesFilter, extractRoutes);

// 首页
router.get('/', (req, res) => res.render('index', { message: '微信第三方服务运行中...' }));
export default router;
